package demoMod.stsconsole.rewards;

import demoMod.stsconsole.ConsoleEntry;
import demoMod.stsconsole.cards.AbstractCard;
import demoMod.stsconsole.dungeons.AbstractDungeon;
import demoMod.stsconsole.helpers.CardHelper;
import demoMod.stsconsole.helpers.LocalizedStrings;
import demoMod.stsconsole.helpers.SubscriberReturnValueSaver;
import demoMod.stsconsole.interfaces.GenerateCardRewardSubscriber;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CardReward extends AbstractReward {
    private final List<AbstractCard> cards;

    public CardReward() {
        this.cards = new ArrayList<>();
        SubscriberReturnValueSaver choices = new SubscriberReturnValueSaver();
        choices.intValue = 3;
        ConsoleEntry.triggerSubscribers(GenerateCardRewardSubscriber.class, "changeNumOfCardChoices",
                new Class[]{int.class}, new Object[]{choices.intValue},
                (method, params, subscriber) -> {
                    try {
                        params[0] = method.invoke(subscriber, params);
                        choices.intValue = (int) params[0];
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
        );
        CardHelper.getRewardCards(choices.intValue, this.cards);
    }

    public CardReward(List<AbstractCard> cards) {
        this.cards = cards;
    }

    @Override
    public boolean claimReward() {
        int index = 0;
        for (AbstractCard card : this.cards) {
            card.calculateCardDamage(null);
            System.out.println(index + "\t" + card.name);
            List<String> cmdStrings = LocalizedStrings.getCommandStrings("Smith");
            System.out.println(cmdStrings.get(card.type.ordinal() + 7));
            System.out.print(cmdStrings.get(3));
            if (card.cost == -1) {
                System.out.println("X");
            } else if (!card.canUse) {
                System.out.println("-");
            } else {
                System.out.println(card.cost);
            }
            System.out.println(card.getUpdatedDescriptions(null));
            index++;
        }
        Scanner sc = ConsoleEntry.getScanner();
        List<String> rewardStrings = LocalizedStrings.getRewardStrings("AbstractReward");
        System.out.print(rewardStrings.get(5));
        String choice = sc.nextLine();
        switch (choice) {
            case "x":
                return false;
            default:
                try {
                    index = Integer.parseInt(choice);
                    if (index < 0 || index >= this.cards.size()) return false;
                    AbstractCard card = this.cards.remove(index);
                    System.out.println(rewardStrings.get(6) + card.name);
                    AbstractDungeon.player.addCardToMasterDeck(card);
                    for (AbstractCard card1 : this.cards) {
                        card1.onRemove();
                    }
                    return true;
                } catch (NumberFormatException e) {
                    return false;
                }
        }
    }

    @Override
    public void preview() {
        List<String> rewardStrings = LocalizedStrings.getRewardStrings("AbstractReward");
        System.out.print(rewardStrings.get(0));
        for (AbstractCard card : this.cards) {
            System.out.print("\t" + card.name);
        }
        System.out.println();
    }

    @Override
    public void skipped() {
        for (AbstractCard card : this.cards) {
            card.onRemove();
        }
    }
}
